<!-- 选择月份查询 -->
<template>
	<view>
		<view class="p-3">
			<view class="bt-block py24px30">
				<bg-title>
					<text class="font-14 font-normal">
						<slot> {{recordUserName}}班组工资表</slot>
					</text>
				</bg-title>
				<view class="flex my-4">
					<view>选择查询月份</view>
				</view>
				<view @click="openPicker" class="mb-4">
				<u-input v-model="year" readonly placeholder="请选择日期">
					<view slot="suffix">
						<u-icon name="arrow-down-fill" size="12"></u-icon>
					</view>
				</u-input>
			</view>
			<view>
				<bg-btn @click="searchHandler">查询</bg-btn>
			</view>
				<!-- <bg-search3 label="选择查询月份" @search="searchHandler">
					<text>{{recordUserName}}班组工资表</text>
				</bg-search3> -->
			</view>
			

			<view v-if="link" class="bt-block" style="" position:relative>
				<bg-webview :src="link"></bg-webview>
			</view>
		</view>
		<lingfeng-timepicker ref="timePop" type="year-month" :defaultData="defaultData" :maxDate="maxDate"
		@change="confirm"></lingfeng-timepicker>
	</view>
</template>

<script>
	import {
		salaryExport,
		attendanceExport
	} from "@/api/excel"
	export default {
		onLoad(options) {
			console.log(options)
			this.workJobType = options.workJobType
			this.recordUserName = options.recordUserName
			this.params.recordUserId = options.recordUserId
			this.params.projectId = options.projectId

			let now = new Date();
			let y = now.getFullYear();
			let m = (now.getMonth() + 1).toString().padStart(2, '0');
			this.year = `${y}${m}`;
			this.defaultData.startTime = `${y}-${m}`;
		},
		data() {
			return {
				link: '',
				recordUserName: '',
				workJobType: '',
				params: {
					workDate: '',
					recordUserId: '',
					projectId: '',
				},
				year: '',
				defaultData: {
					month: ''
				},
				maxDate: {
					year: 2030,
					month: 12,
					date: 31
				}
			}
		},
		methods: {
			searchHandler(e) {
				// this.params.workDate = e
				this.params.workDate = this.year
				console.log(e)
				this.getExcelData()
			},
			getExcelData() {
				salaryExport(this.params).then(res => {
					this.link = res.data
				})
			},
			openPicker() {
				console.log(this.defaultData.startTime)

				// 确保在显示选择器之前设置正确的默认值
				if (this.year) {
					const y = this.year.slice(0, 4);
					const m = this.year.slice(4, 6);
					this.defaultData.startTime = `${y}-${m}`;
					this.defaultData.month = `${y}-${m}`; // 对于year-month类型，需要同时设置month
				} else {
					const now = new Date();
					const y = now.getFullYear();
					const m = (now.getMonth() + 1).toString().padStart(2, '0');
					this.defaultData.startTime = `${y}-${m}`;
					this.defaultData.month = `${y}-${m}`;
				}
				
				this.$refs.timePop.show();
			},
			confirm(e) {
				console.log(e)
				this.year = e.replace(/-/g, '');
				this.defaultData.startTime = e;
				this.defaultData.month = e; // 对于year-month类型，需要同时设置month
				this.defaultData.endTime = e;
			},
		}
	}
</script>

<style>

</style>